package br.com.sgmf.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import br.com.sgmf.model.Motorista;

@Repository
public class MotoristaDAO {

	@PersistenceContext
	private EntityManager manager;

	@Transactional
	public void save(Motorista motorista) {
		manager.persist(motorista);
	}

	@Transactional
	public void update(Motorista motorista) {
		manager.merge(motorista);
	}

	@Transactional
	public void delete(Motorista motorista) {
		motorista = manager.merge(motorista);
		manager.remove(motorista);
	}

	@Transactional(readOnly = true)
	public Motorista findById(Integer id) {
		return manager.find(Motorista.class, id);
	}

	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<Motorista> findAll() {
		return manager.createQuery("select m from Motorista m").getResultList();
	}

}
